package lib;
import java.security.Key;
import java.util.HashMap;
import java.util.Objects;

/*
HashMap<String, String> Sites = new HashMap<String, String>();
Sites.put("one", "Google");
Sites.put("two", "Runoob");
C.var_dump( MysqlStmt.table("beinan").field("id,name").where("uid = 1 and id = 5").order("id desc").find());
C.var_dump( MysqlStmt.table("beinan").field("id,name").where("uid = 1 and id = 5").order("id desc").limit(5).get());
C.var_dump( MysqlStmt.table("cs").insert(Sites));
C.var_dump( MysqlStmt.table("cs").where("uid = 1 and id = 5").update(Sites));
C.var_dump( MysqlStmt.table("cs").delete());
 */
public class MysqlStmt {
    String str_table,str_where,str_order;
    String str_field = "*";

    Object str_limit;
    public static MysqlStmt table(String val){

        return new MysqlStmt(val);
    }

    public MysqlStmt(String val){
        str_where = str_order = null;
        str_field = "*";
        str_table = val;
    }

    public MysqlStmt field(String val){
        str_field = val;
        return this;
    }

    public MysqlStmt where(String val){
        str_where = val;
        return this;
    }

    public MysqlStmt order(String val){
        str_order = val;
        return this;
    }
    public MysqlStmt limit(Object val){
        str_limit = val;
        return this;
    }
    public String insert(HashMap<String, String> Sites){
        StringBuilder key_name = new StringBuilder("(");
        StringBuilder key_value = new StringBuilder("(");
        for (String i : Sites.keySet()) {
            key_name.append(i).append(",");
            key_value.append(Sites.get(i)).append(",");
        }
        key_name = new StringBuilder(key_name.substring(0, key_name.length() - 1));
        key_name.append(")");

        key_value = new StringBuilder(key_value.substring(0, key_value.length() - 1));
        key_value.append(")");
        return  "insert into "+str_table+key_name+" values"+key_value;
    }
    public String update(HashMap<String,String> Sites){
        StringBuilder key_name = new StringBuilder("");
        for (String i : Sites.keySet()) {
            key_name.append(i).append("=").append(Sites.get(i)).append(",");
        }
        key_name = new StringBuilder(key_name.substring(0, key_name.length() - 1));
        String sql = "update "+str_table +" set "+key_name.toString();
        if(str_where != null){
            sql += " where "+str_where;
        }
        return  sql;
    }
    public String delete(){
        String sql = "delete from "+str_table ;
        if(str_where != null){
            sql += " where "+str_where;
        }
        return sql;
    }

    public String find(){
        str_limit = 1;
        return this.get();
    }
    public String get(){
        String sql = "select " + str_field + " from " + str_table;
        if(str_where != null){
            sql += " where "+str_where;
        }
        if(str_order != null){
            sql += " order "+str_order;
        }
        if(str_limit != null){
            sql += " limit "+str_limit;
        }
        //select id from  cs
        return  sql;
    }

}
